using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using ZoneFiveSoftware.Common.Data.Fitness;
using FilteredStatistics.Controller;
using FilteredStatistics.Common;
using ZoneFiveSoftware.Common.Data;

namespace FilteredStatistics.Data
{
    class PostProcessNamedZone : INamedZone
    {
        public PostProcessNamedZone(IPostProcessFilter filter)
        {
            m_Filter = filter;

            m_ActivityCallbackHelper = new ActivityDataChangedHelper(null);
            m_ActivityCallbackHelper.PropertyChanged += new PropertyChangedEventHandler(OnActivityDataChanged);
        }

        void OnActivityDataChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Activity.Duration")
            {
                TriggerValidTimesChanged();
            }
        }

#region INamedZone members

        public String Name
        {
            get { return m_Filter.DisplayString; }
        }

        public IValueRangeSeries<DateTime> ValidTimes
        {
            get
            {
                return (ValueRangeSeries<DateTime>)WholeActivityFilteredDataSet.Instance.GetFilteredTimes();
            }
        }

        public event PropertyChangedEventHandler ValidTimesChanged;

#endregion

        public override int GetHashCode()
        {
            return m_Filter.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            PostProcessNamedZone namedZone = obj as PostProcessNamedZone;

            if (namedZone != null)
            {
                return m_Filter.Equals(namedZone.m_Filter);
            }

            return false;
        }

        protected void TriggerValidTimesChanged()
        {
            if (ValidTimesChanged != null)
            {
                ValidTimesChanged(this, new PropertyChangedEventArgs("ValidTimes"));
            }
        }

        public IPostProcessFilter PostProcessFilter
        {
            get { return m_Filter; }
        }

        private ActivityDataChangedHelper m_ActivityCallbackHelper = null;
        private IPostProcessFilter m_Filter = null;
    }
}
